package com.excesys.exsecs.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.admin.api.entity.SysCompConf;
import com.excesys.exsecs.admin.api.entity.SysPageCompRel;
import com.excesys.exsecs.admin.api.entity.SysPageDesign;
import com.excesys.exsecs.admin.mapper.SysCompConfMapper;
import com.excesys.exsecs.admin.mapper.SysPageCompRelMapper;
import com.excesys.exsecs.admin.mapper.SysPageDesignMapper;
import com.excesys.exsecs.admin.service.SysPageDesignService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统页面设计
 *
 * @author lxc
 * @date 2024-11-18 14:06:29
 */
@Service
@RequiredArgsConstructor
public class SysPageDesignServiceImpl extends ServiceImpl<SysPageDesignMapper, SysPageDesign> implements SysPageDesignService {

    private final SysPageCompRelMapper sysPageCompRelMapper;
    private final SysCompConfMapper sysCompConfMapper;

    @Override
    public Page<SysPageDesign> queryPage(Page page, SysPageDesign sysPageDesign) {
        LambdaQueryWrapper<SysPageDesign> wrapper = Wrappers.lambdaQuery();
        Page<SysPageDesign> page1 = this.page(page, wrapper);
        if (ObjectUtil.isNotEmpty(page1) && CollectionUtils.isNotEmpty(page1.getRecords())) {
            page1.getRecords().forEach(design ->{
                List<SysPageCompRel> rels = sysPageCompRelMapper.selectList(Wrappers.<SysPageCompRel>lambdaQuery().eq(SysPageCompRel::getPageId, design.getId()));
                if (CollectionUtils.isNotEmpty(rels)) {
                    List<JSONObject> arr = new ArrayList<>();
                    List<String> compNameList = new ArrayList<>();
                    rels.forEach(rel ->{
                        JSONObject extra = rel.getExtra();
                        if (ObjectUtil.isNotEmpty(extra)) {
                            SysCompConf comp = sysCompConfMapper.selectById(extra.getInt("compId"));
                            if (ObjectUtil.isNotEmpty(comp)) {
                                extra.set("compZh", comp.getCompZh());
                                extra.set("compEn", comp.getCompEn());
                            }
                            compNameList.add(comp.getCompZh());
                        }
                        arr.add(extra);
                    });
                    design.setRelList(arr);
                    if (CollectionUtils.isNotEmpty(compNameList)) {
                        design.setCompNames(String.join(",", compNameList));
                    }
                }
            });
        }
        return page1;
    }

    @Override
    public SysPageDesign getDesign(Integer id) {
        SysPageDesign design = this.getById(id);
        List<SysPageCompRel> rels = sysPageCompRelMapper.selectList(Wrappers.<SysPageCompRel>lambdaQuery().eq(SysPageCompRel::getPageId, id));
        if (CollectionUtils.isNotEmpty(rels)) {
            List<JSONObject> arr = new ArrayList<>();
            rels.forEach(rel ->{
                JSONObject extra = rel.getExtra();
                if (ObjectUtil.isNotEmpty(extra)) {
                    SysCompConf comp = sysCompConfMapper.selectById(extra.getInt("compId"));
                    if (ObjectUtil.isNotEmpty(comp)) {
                        extra.set("compZh", comp.getCompZh());
                    }
                }
                arr.add(extra);
            });
            design.setRelList(arr);
        }
        return design;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDesign(SysPageDesign sysPageDesign) {
        boolean save = this.save(sysPageDesign);
        if (CollectionUtils.isNotEmpty(sysPageDesign.getRelList())){
            sysPageDesign.getRelList().forEach(json ->{
                SysPageCompRel rel = new SysPageCompRel();
                rel.setPageId(sysPageDesign.getId());
                rel.setCompId(json.getInt("compId"));
                json.set("pageId", sysPageDesign.getId());
                rel.setExtra(json);
                sysPageCompRelMapper.insert(rel);
            });
        }
        return save;
    }

    @Override
    public boolean updateDesign(SysPageDesign sysPageDesign) {
        boolean update = this.updateById(sysPageDesign);
        if (CollectionUtils.isNotEmpty(sysPageDesign.getRelList())){
                sysPageCompRelMapper.delete(Wrappers.<SysPageCompRel>lambdaQuery().eq(SysPageCompRel::getPageId, sysPageDesign.getId()));
                sysPageDesign.getRelList().forEach(json ->{
                    SysPageCompRel rel = new SysPageCompRel();
                    rel.setPageId(sysPageDesign.getId());
                    rel.setCompId(json.getInt("compId"));
                    json.set("pageId", sysPageDesign.getId());
                    rel.setExtra(json);
                    sysPageCompRelMapper.insert(rel);
                });
            }
        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchDesign(List<Integer> list) {
        list.forEach(it ->{
            sysPageCompRelMapper.delete(Wrappers.<SysPageCompRel>lambdaQuery().eq(SysPageCompRel::getPageId, it));
        });
        return this.removeByIds(list);
    }
}